package com.sskj.b70.p.fragment;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.rxbus2.RxBus;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.StockCoinBean;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.bean.StockBean;
import com.sskj.b70.m.bean.SysConfigBean;
import com.sskj.b70.m.bean.TradeInfoBean;
import com.sskj.b70.m.bean.TransactionModel;
import com.sskj.b70.m.bean.UserInfo;
import com.sskj.b70.m.bean.rxbus.StockLevel;
import com.sskj.b70.m.http.ExceptionUtil;
import com.sskj.b70.m.http.JsonCallBack;
import com.sskj.b70.m.http.JsonConverter;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.DisposUtil;
import com.sskj.b70.utils.SPUtils;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.fragment.LevelTransactionFragment;

import java.math.BigDecimal;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;

/**
 * Created by Administrator on 2018/4/9 0009.
 */

public class LevelTransactionFragmentPst extends BasePresenter<LevelTransactionFragment> {

    private Disposable disposable;

    public void getPankou(String code) {
        httpService.getPankou(code)
                .tag(this)
                .execute(new JsonCallBack<HttpData<TransactionModel>>() {
                    @Override
                    protected void _onSuccess(Response<HttpData<TransactionModel>> response) {
                        mView.setBuySell(response.body().getData());
                    }
                });
    }

    public void getUserInfo() {
        httpService.getUserInfo()
                .tag(this)
                .execute(new JsonCallBack<HttpData<UserInfo>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.STATUS_OK && httpData.getData() != null) {
                            RxBus.getDefault().post(httpData.getData());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, true);
                            mView.updateWallet(httpData.getData().getWallone());
                        }
                    }
                });
    }

    public void createOrder(StockBean stockData, boolean isTime, boolean isWeituo) {

        httpService.createLevelOrder(stockData, stockData.buynum, stockData.newprice, stockData.code, stockData.type, stockData.otype == 1 ? 2 : 1, stockData.stopwin, stockData.stoploss, isTime)
                .tag(this)
                .execute(new JsonCallBack<HttpData>(this, -1) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData HttpData = response.body();
                        ToastUtil.showShort(HttpData.getMsg());
                        if (HttpData.status == HttpConfig.STATUS_OK) {
                            mView.onCreateOrderSuccess(isWeituo);
                        } else {
                            ToastUtil.showShort(HttpData.msg);
                            mView.onCreateOrderFail();

                        }
                    }
                });
    }


    public void getStockCoinByCode(String code) {
        mView.mActivity.showLoading();
        Flowable<HttpData<List<StockCoinBean>>> stockFlow = httpService.getProduct(code)
                .converter(new JsonConverter<HttpData<List<StockCoinBean>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                }).adapt(new FlowableBody<>());
        Flowable<HttpData<List<SysConfigBean>>> sysFlow = httpService.getSysConfig(5)
                .converter(new JsonConverter<HttpData<List<SysConfigBean>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                })
                .adapt(new FlowableBody<>());
        disposable = Flowable.zip(stockFlow, sysFlow, new BiFunction<HttpData<List<StockCoinBean>>, HttpData<List<SysConfigBean>>, StockLevel>() {
            @Override
            public StockLevel apply(HttpData<List<StockCoinBean>> listHttpData, HttpData<List<SysConfigBean>> listHttpData2) throws Exception {
                List<StockCoinBean> data = listHttpData.getData();
                StockLevel stockLevel = new StockLevel();
                Flowable.fromIterable(listHttpData2.getData())
                        .filter(item -> item.getName().equals("lever_deal_fee"))
                        .subscribe(item -> stockLevel.setShouPer(new BigDecimal(item.getValue()).doubleValue()));
                StockCoinBean stock = data.get(0);
                stockLevel.setCode(stock.getCode());
                stockLevel.setName(stock.getName());
                stockLevel.setLeverage(stock.getLever());
                stockLevel.setMinDealNum(stock.getContractMin());
                stockLevel.setPrice(Double.valueOf(stock.getStockProductVO().getPrice()));
                stockLevel.setSlidingScalePrice(stock.getSlidingScalePrice());
                stockLevel.setSpread(stock.getSpread());
                stockLevel.setStopLoss((float) stock.getStopLoss());
                stockLevel.setStopProfit((float) stock.getStopProfit());
                stockLevel.setMinStopLoss((float) stock.getMinStopLoss());
                stockLevel.setMinStopProfit((float) stock.getMinStopProfit());
                return stockLevel;
            }
        }).subscribe(stockLevel -> {
            mView.mActivity.hideLoading();
            mView.setStockLevel(stockLevel);
        }, e -> {
            mView.mActivity.hideLoading();
            ExceptionUtil.dealException(e);
        });
    }

    public void getRisk() {
        if (MyAppLication.isLogin()) {
            httpService.getCapitalInfo()
                    .tag(this)
                    .execute(new JsonCallBack<HttpData<TradeInfoBean>>(false) {
                        @Override
                        public void _onSuccess(Response<HttpData<TradeInfoBean>> response) {
                            HttpData<TradeInfoBean> httpListData = response.body();
                            if (httpListData.getStatus() == HttpConfig.STATUS_OK) {
                                if (mView != null) {
                                    mView.updateRisk(httpListData.getData());
                                }
                            }
                        }
                    });
        }
    }

    /**
     * 获取股票最新数据
     *
     * @param
     * @param code 编码
     */
    public void getNewInfo(String code) {
        httpService.getProduct(code)
                .tag(this)
                .execute(new JsonCallBack<HttpData<List<StockCoinBean>>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<List<StockCoinBean>>> response) {
                        mView.updateUI(response.body().getData().get(0).getStockProductVO());
                    }
                });
    }

    @Override
    public void detachView() {
        DisposUtil.close(disposable);
        super.detachView();
    }
}
